/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.pconline.activity.util;

/**
 *
 * @author pc
 */
public class OutputUtils {

    public static String toHTML(String str, String enter, boolean exceptEm) {
        if (str == null || str.equals("")) {
            return "";
        }
        if (exceptEm) {
            str = str.replaceAll("<img src=http://www1.pcbaby.com.cn/sns/images/comment/face([0-9]*).gif>", "#img$1#");
        }
        str = replace(str, "&", "&amp;");
        str = replace(str, "<", "&lt;");
        str = replace(str, ">", "&gt;");
        str = replace(str, "  ", "&nbsp;&nbsp;");
        str = replace(str, " ", "&nbsp;");
        str = replace(str, "\r\n", "\n");
        str = replace(str, "\n", enter);
        str = replace(str, "\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
        str = replace(str, "'", "��");
        str = replace(str, "\"", "��");
        str = replace(str, "'", "&#039;");
        str = replace(str, "\"", "&#034;");
        if (exceptEm) {
            str = str.replaceAll("#img([0-9]*)#", "<img src=http://www1.pcbaby.com.cn/sns/images/comment/face$1.gif>");
        }
        return str;
    }

    public static String replace(String text, String repl, String with) {
        return replace(text, repl, with, -1);
    }

    public static String replace(String text, String repl, String with, int max) {
        if (text == null || repl == null || with == null || repl.length() == 0
                || max == 0) {
            return text;
        }
        StringBuffer buf = new StringBuffer(text.length());
        int start = 0, end = 0;
        while ((end = text.indexOf(repl, start)) != -1) {
            buf.append(text.substring(start, end)).append(with);
            start = end + repl.length();
            if (--max == 0) {
                break;
            }
        }
        buf.append(text.substring(start));
        return buf.toString();
    }
}
